package com.example.demo.webflux.controller;

import com.example.demo.webflux.MyWebFluxService;
import com.example.demo.webflux.entity.UserEntity;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

/**
 * webflux接口Api
 *
 * @author huangchangjin
 * @date 2025/7/16 9:57
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/webflux")
public class ApiWebFluxController {

    @Resource
    private MyWebFluxService myWebFluxService;


    @RequestMapping("/test")
    public Flux<Map<String, Object>> test() {
        // 测试webflux 查询db数据，并返回结果
        Flux<Map<String, Object>> flux = Flux.just(Map.of("msg", "hello webflux"));
        flux.subscribe(data -> log.info("data: {}", data));
        // 阻塞等待所有数据处理完成
        flux.blockLast();
        return flux;
    }
    @RequestMapping("/list")
    public Flux<UserEntity> list() {
        Flux<UserEntity> all = myWebFluxService.findAll();
        return all;
    }
}